package git.yyge.test.entity;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

public class StudentGradeGenerator {
    public static void main(String[] args) {
        // 定义学生姓名数组
        String[] students = {
            "张三", "李四", "王五", "赵六", "钱七",
            "孙八", "周九", "吴十", "郑一", "王二",
            "冯三", "陈四", "褚五", "卫六", "蒋七",
            "沈八", "韩九", "杨十", "朱一", "秦二"
        };

        // 定义科目
        String[] subjects = {"语文", "数学", "英语", "物理", "化学"};

        // 生成成绩文件
        generateGradeFile("student_grades.txt", students, subjects);
    }

    /**
     * 生成学生成绩文件
     * @param filename 文件名
     * @param students 学生姓名数组
     * @param subjects 科目数组
     */
    public static void generateGradeFile(String filename, String[] students, String[] subjects) {
        try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
            Random random = new Random();

            // 写入文件头
            writer.write("学生成绩单");
            writer.newLine();
            writer.write("=====================================");
            writer.newLine();
            writer.newLine();

            // 为每个学生生成成绩
            for (String student : students) {
                StringBuilder line = new StringBuilder();
                line.append("学生: ").append(student);

                // 为每个科目生成随机成绩
                int totalScore = 0;
                for (String subject : subjects) {
                    int score = random.nextInt(51) + 50; // 生成50-100之间的随机分数
                    line.append(" | ").append(subject).append(": ").append(score);
                    totalScore += score;
                }

                // 计算平均分
                double average = (double) totalScore / subjects.length;
                line.append(" | 总分: ").append(totalScore);
                line.append(" | 平均分: ").append(String.format("%.2f", average));

                writer.write(line.toString());
                writer.newLine();
            }

            System.out.println("成绩文件已成功生成: " + filename);

        } catch (IOException e) {
            System.err.println("生成文件时出错: " + e.getMessage());
        }
    }
}
